package kr.ac.kongju.gedl.item.web;

import java.io.File;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import kr.ac.kongju.gedl.attach.service.AttachFileService;
import kr.ac.kongju.gedl.attach.vo.AttachFileVO;
import kr.ac.kongju.gedl.board.service.BoardContentsService;
import kr.ac.kongju.gedl.board.vo.ContentsVO;
import kr.ac.kongju.gedl.cmmn.web.CU;
import kr.ac.kongju.gedl.code.vo.CodeInfoVO;
import kr.ac.kongju.gedl.common.util.ListResultVO;
import kr.ac.kongju.gedl.item.service.ItemService;
import kr.ac.kongju.gedl.item.vo.ItemInfoVO;
import kr.ac.kongju.gedl.item.vo.ItemVO;

import org.apache.commons.lang.StringUtils;
import org.dozer.DozerBeanMapperSingletonWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;

import egovframework.rte.fdl.property.EgovPropertyService;
import egovframework.rte.ptl.mvc.tags.ui.pagination.DefaultPaginationManager;
import egovframework.rte.ptl.mvc.tags.ui.pagination.PaginationInfo;
import egovframework.rte.ptl.mvc.tags.ui.pagination.PaginationManager;
import egovframework.rte.ptl.mvc.tags.ui.pagination.PaginationRenderer;

@Controller
public class ItemController {
	
	/** EgovPropertyService */
    @Resource(name = "propertiesService")
    protected EgovPropertyService propertiesService;
    
    @Resource(name = "itemService")
    protected ItemService itemService;
    
    @Resource(name = "ajaxResultView")
	protected MappingJacksonJsonView ajaxResultView;
    
    @Resource(name="paginationManager")
    protected PaginationManager paginationManager;
    
    @Resource(name = "attachFileService")
    protected AttachFileService attachFileService;
    
    private final Logger logger = LoggerFactory.getLogger(getClass());
    
    public static final String DEFAULT_AREA_CD = "R015";
    private final int PAGE_SIZE = 3;
    private final int ROW_PER_PAGE = 100;
    private String type = "image";
	private String jsFunction = "fncPage";
    
    
    @RequestMapping(value="/item/map.do")
	public String selectMap(@ModelAttribute("searchVO") ItemVO searchVO, 
			ModelMap model, SessionStatus status, HttpSession session) throws Exception {
    	
    	searchVO.setPageSize(PAGE_SIZE);
    	searchVO.setRowPerPage(ROW_PER_PAGE);
    	
    	ListResultVO<ItemVO> resultList = itemService.retrieveItemList(searchVO);
		
    	List<CodeInfoVO> regionList1 = CU.getInstance().getCodeList("REGION");
    	
    	String areaCd = searchVO.getAreaCd();
    	
    	if(StringUtils.isEmpty(areaCd)) {
    		areaCd = DEFAULT_AREA_CD;
    		searchVO.setAreaCd(areaCd);
    	}
    	
    	if(areaCd.length() >= 4) {
	    	List<CodeInfoVO> regionList2 = CU.getInstance().getCodeList(areaCd.substring(0, 4));
			model.addAttribute("regionList2", regionList2);
    	}
		
    	if(areaCd.length() >= 7) {
    		List<CodeInfoVO> regionList3 = CU.getInstance().getCodeList(areaCd.substring(0, 7));
    		model.addAttribute("regionList3", regionList3);
    	}
    	
    	//페이징을 구성한다
    	PaginationInfo paginationInfo = new PaginationInfo();

		paginationInfo.setCurrentPageNo(searchVO.getPage());
		paginationInfo.setRecordCountPerPage(searchVO.getRowPerPage());
		paginationInfo.setPageSize(searchVO.getPageSize());

		paginationInfo.setTotalRecordCount(resultList.getListCount());
    	
		
    	model.addAttribute("regionList1", regionList1);
		model.addAttribute("searchVO", searchVO);
		model.addAttribute("resultList", resultList);
		model.addAttribute("paginationInfo", paginationInfo);
		
    	return "/item/mapViewer";
	}
    
    @RequestMapping(value="/item/changeArea.do", method=RequestMethod.GET)
	public ModelAndView selecArea(@ModelAttribute("searchVO") ItemVO searchVO, SessionStatus status) throws Exception {
		
    	Map<String, Object> model = new HashMap<String, Object>();
    	
    	String areaCd = searchVO.getAreaCd();
    	
    	List<CodeInfoVO> regionList = CU.getInstance().getCodeList(areaCd);
		model.put("regionList", regionList);
    	
		status.setComplete();
		
		return new ModelAndView(ajaxResultView, model); 
	}
    
    
    
    @RequestMapping(value="/item/itemList.do", method=RequestMethod.GET)
	public ModelAndView selectItemList(@ModelAttribute("searchVO") ItemVO searchVO, SessionStatus status) throws Exception {
		
    	searchVO.setPageSize(PAGE_SIZE);
		searchVO.setRowPerPage(ROW_PER_PAGE);
    	
    	Map<String, Object> model = new HashMap<String, Object>();
    	
		ListResultVO<ItemVO> resultList = itemService.retrieveItemList(searchVO);
		
		//페이징을 구성한다
		PaginationInfo paginationInfo = new PaginationInfo();

		paginationInfo.setCurrentPageNo(searchVO.getPage());
		paginationInfo.setRecordCountPerPage(searchVO.getRowPerPage());
		paginationInfo.setPageSize(searchVO.getPageSize());

		paginationInfo.setTotalRecordCount(resultList.getListCount());
		
		PaginationRenderer paginationRenderer = paginationManager.getRendererType(type);
        
        String paginationContents = paginationRenderer.renderPagination(paginationInfo, jsFunction);
        
		model.put("resultList", resultList);
		model.put("paginationContents", paginationContents);
		status.setComplete();
		
		return new ModelAndView(ajaxResultView, model); 
	}
    
    
    @RequestMapping(value="/item/itemData.do", method=RequestMethod.GET)
	public String selectItemData(@ModelAttribute("searchVO") ItemVO searchVO, 
			@RequestParam(value="viewType", required=true) String viewType, ModelMap model, SessionStatus status) throws Exception {
    	
    	ItemVO result = itemService.retrieveItemInfo(searchVO);
    	AttachFileVO attachFile = new AttachFileVO();
    	attachFile.setContentsId(result.getItemId());
    	result.setAttachFileList(attachFileService.selectListAttachFile(attachFile));
        
		model.addAttribute("result", result);
		status.setComplete();
		
		String url = "/item/detailPopup";
		
		switch(viewType) {
		case "01" :
			url = "/item/detailPopup";
			break;
		case "02" :
			url = "/item/layerPopup";
			break;
		case "03" :
			url = "/item/mapPopup";
			break;
		default :
			url = "/item/detailPopup";
		}
		
		return url; 
	}
    
    
    
}
